package com.multiplepb.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 */
public class TimeUtils {

    public static Integer invertDateToInteger(Date date) {
        Long l = date.getTime() / 1000;
        return l.intValue();
    }
    public static Integer getSeconds(String timeString) {
        // 使用冒号分割字符串以获取分钟和秒
        String[] parts = timeString.split(":");

        // 确保分割后的数组长度为2
        if (parts.length == 2) {
            // 将分钟和秒转换为整数
            int minutes = Integer.parseInt(parts[0]);
            int seconds = Integer.parseInt(parts[1]);

            // 计算总秒数
            int totalSeconds = minutes * 60 + seconds;
            return totalSeconds;
        } else {
            System.out.println("时间格式不正确");
        }
        return null;
    }
    public static String getDelayDate(int delayDays) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, delayDays);
        date = calendar.getTime();
        System.out.println(sdf.format(date));
        return sdf.format(date);
    }
    public static int getMonIntervalFromNow(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now(); // 结束日期为当前日期
        Period period = Period.between(nowDate,localDate);
        int months = period.getYears() * 12 + period.getMonths();
        return months;
    }
    public static String formatYYYY_MM_DDNo0(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(date);
        String[] dateComponents = formattedDate.split("-");
        String newDate = Integer.parseInt(dateComponents[0]) + "-" + Integer.parseInt(dateComponents[1]) + "-" + Integer.parseInt(dateComponents[2]);
        return newDate;
    }
    //生成日期格式为：2023年2月1日
    public static String formatYYYY_MM_DDNo0WithHZ(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(date);
        String[] dateComponents = formattedDate.split("-");
        String newDate = Integer.parseInt(dateComponents[0]) + "年" + Integer.parseInt(dateComponents[1]) + "月" + Integer.parseInt(dateComponents[2]) + "日";
        return newDate;
    }
    //生成日期格式2023010222
    public static String formatYYYY_MM_DD_SS(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String formattedDate = dateFormat.format(date);
        return formattedDate;
    }

    //五年前的今天的下一天
    public static Date get5YearBefore() {
        Calendar calendar = Calendar.getInstance(); // 获取当前时间
        calendar.add(Calendar.YEAR, -5); // 将时间减去五年
        calendar.add(Calendar.DAY_OF_MONTH, 1); // 再加一天;
        Date fiveYearsAgo = calendar.getTime(); // 获取五年前的日期
        return fiveYearsAgo;
    }

    public static String formatYYYY_MM_DD(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String formattedDate = dateFormat.format(date);
        return formattedDate;
    }
    public static String formatYYYYMMDD(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String formattedDate = dateFormat.format(date);
        return formattedDate;
    }

    public static int getDateIntervalFromNow(LocalDate localDate) {
        LocalDate nowDate = LocalDate.now(); // 结束日期为当前日期
        long daysBetween = ChronoUnit.DAYS.between(nowDate, localDate);
        LocalTime currentTime = LocalTime.now();
        LocalTime twentyThree = LocalTime.of(22, 0);

        if (currentTime.isAfter(twentyThree)) {
            return (int) daysBetween - 1;
        }
        return (int) daysBetween;
    }
    public static String formatYYYY_MM_DD(LocalDate localDate) {
        String format = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        return format;
    }
    public static String formatYYYYXMMXDD(LocalDate localDate) {
        String format = localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        return format;
    }

    public static void main(String[] args) {

        getDelayDate(2);


    }
}
